#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/6/13 21:56
# @Author : 闫峰
# @File    : batch_pull.py
import subprocess
from utils.otherutils.MyThreadPool import MyThreadPool


class TestGitPull:

    @staticmethod
    def test_git_pull():
        service_name_list = ["zhcxkj-application-common",
                             "zhcxkj-application-open",
                             "zhcxkj-application-publish",
                             "zhcxkj-application-strategy",
                             "zhcxkj-bus-aliexpress",
                             "zhcxkj-bus-allegro",
                             "zhcxkj-bus-common",
                             "zhcxkj-bus-download",
                             "zhcxkj-bus-shopee",
                             "zhcxkj-bus-strategy",
                             "zhcxkj-bus-temu",
                             "zhcxkj-bus-walmart",
                             "zhcxkj-center-account",
                             "zhcxkj-center-attribute",
                             "zhcxkj-center-category",
                             "zhcxkj-center-data",
                             "zhcxkj-center-datasync",
                             "zhcxkj-center-datasync-comprehensive",
                             "zhcxkj-center-listing-comprehensive",
                             "zhcxkj-center-download",
                             "zhcxkj-center-logger",
                             "zhcxkj-center-message",
                             "zhcxkj-center-order",
                             "zhcxkj-center-product",
                             "zhcxkj-center-promotion",
                             "zhcxkj-center-strategy",
                             "zhcxkj-center-strategy-comprehensive",
                             "zhcxkj-center-upc",
                             "zhcxkj-center-warehouse",
                             "zhcxkj-common",
                             "zhcxkj-common-thirdapi",
                             "zhcxkj-parent",
                             "zhcxkj-comprehensive-structure",
                             "zhcxkj-gateway"
                             ]
        # command_list = ["cd D:/java && git branch --set-upstream-to=origin/test test"]
        # command_list = ["cd D:/code/java && git branch --set-upstream-to=origin/test test"]
        command_list = []
        for service_name in service_name_list:
            command_list.append(f"cd D:/code/java && cd {service_name}/ && git checkout test && git pull && cd ..")
            # command_list.append(f"cd D:/code/java && cd {service_name}/ && git pull origin/test test && cd ..")
        MyThreadPool(8).multithreaded(TestGitPull.execute_commands, command_list)

    @staticmethod
    def execute_commands(command):
        # 使用subprocess.run()执行命令
        result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

        # 获取标准输出和标准错误输出
        stdout = result.stdout
        stderr = result.stderr

        # 检查返回码
        if result.returncode == 0:
            pass
            print(f"{command}命令执行成功")
            # print("命令执行成功，输出如下：")
            # print(stdout)
        else:
            print(f"{command}命令执行失败，错误信息如下：")
            print(stderr)

if __name__ == '__main__':
    TestGitPull.test_git_pull()